<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Activation;

use LogicException;
use SplObjectStorage;
use Slipstream\Configuration\ContextInterface;
use Slipstream\ContainerInterface;

/**
 *
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class BaseSession implements SessionInterface
{
	/**
	 * @var bool
	 */
	protected $collected = false;
	
	/**
	 * @var ContainerInterface
	 */
	protected $container;
	
	/**
	 * @var ContributorRegistry
	 */
	protected $contributorRegistry;
	
	/**
	 * @var string
	 */
	protected $identifier;
	
	/**
	 * @var object
	 */
	protected $instance;
	
	/**
	 * @var SplObjectStorage<PolicyInterface>
	 */
	protected $policies;
	
	/**
	 * @var SessionStage
	 */
	protected $stage;
	
	/**
	 * @var ContextInterface
	 */
	protected $targetContext;
	
	/**
	 * Class constructor
	 *
	 * @param string $identifier
	 * @param ContextInterface $context
	 * @param ContainerInterface $container
	 * @return void
	 */
	public function __construct($identifier, ContextInterface $context, ContainerInterface $container)
	{
		$this->container = $container;
		$this->identifier = $identifier;
		$this->targetContext = $context->getChildContext($identifier);
		
		$this->contributorRegistry = new ContributorRegistry();
		$this->policies = new SplObjectStorage();
		$this->stage = SessionStage::PreCollection();
	}
	
	/**
	 *
	 * @return ContainerInterface
	 */
	public function getContainer()
	{
		return $this->container;
	}
	
	/**
	 *
	 * @return ContributorRegistry
	 */
	public function getContributorRegistry()
	{
		return $this->contributorRegistry;
	}
	
	/**
	 *
	 * @return string
	 */
	public function getIdentifier()
	{
		return $this->identifier;
	}
	
	/**
	 *
	 *
	 * @throws LogicException If instance not set
	 * @return object
	 */
	public function getInstance()
	{
		if(is_null($this->instance)) {
			throw new LogicException('Instance not set');
		}
		return $this->instance;
	}
	
	/**
	 *
	 * @return bool
	 */
	public function hasInstance()
	{
		return (!is_null($this->instance));
	}
	/**
	 *
	 *
	 * @param object $instance
	 * @return void
	 */
	public function setInstance($instance)
	{
		$this->instance = $instance;
	}
	
	/**
	 *
	 *
	 * @param SplObjectStorage<PolicyInterface> $policies
	 * @return void
	 */
	public function addPolicies(SplObjectStorage $policies)
	{
		$this->policies->addAll($policies);
		$this->collected = true;
	}
	
	/**
	 *
	 *
	 * @throws LogicException If no policies have been collected yet
	 * @return SplObjectStorage<PolicyInterface>
	 */
	public function getPolicies()
	{
		if(!$this->collected) {
			throw new LogicException('Policies not collected');
		}
		return $this->policies;
	}
	
	/**
	 *
	 *
	 * @throws LogicException If no policies have been collected yet
	 * @param string $type
	 * @return SplObjectStorage<PolicyInterface>
	 */
	public function getPoliciesByType($type)
	{
		if(!$this->collected) {
			throw new LogicException('Policies not collected');
		}
		
		$policies = new SplObjectStorage();
		foreach($this->policies as $policy) {
			if($policy instanceof $type) {
				$policies->attach($policy);
			}
		}
		return $policies;
	}
	
	/**
	 *
	 *
	 * @throws LogicException If no policies have been collected yet
	 * @param string $type
	 * @return PolicyInterface
	 */
	public function getPolicyByType($type)
	{
		if(!$this->collected) {
			throw new LogicException('Policies not collected');
		}
		
		foreach($this->policies as $policy) {
			if($policy instanceof $type) {
				return $policy;
			}
		}
	}
	
	/**
	 *
	 * @return SessionStage
	 */
	public function getStage()
	{
		return $this->stage;
	}
	
	/**
	 *
	 *
	 * @param SessionStage $stage
	 * @return void
	 */
	public function setStage(SessionStage $stage)
	{
		$this->stage = $stage;
	}
	
	/**
	 *
	 * @return ContextInterface
	 */
	public function getTargetContext()
	{
		return $this->targetContext;
	}
}